home *** CD-ROM | disk | FTP | other *** search
- Path: news.netgate.net!news
- From: Tamara Johnson <malihini@netgate.net>
- Newsgroups: comp.lang.c
- Subject: Re: Question - C (beginner)
- Date: Sun, 25 Feb 1996 13:42:16 -0800
- Organization: NetGate Communications
- Message-ID: <3130D7B8.65B6@netgate.net>
- References: <312FBA11.3946@netgate.net>
- NNTP-Posting-Host: d6.netgate.net
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0b6a (Win95; I)
-
- Tamara Johnson wrote:
- >
- > Problem: not correctly counting chars in
- > each word, not correctly counting words
- > with >0 && <7 chars and words with >6 chars.
- >
- > Suggestions greatly appreciated,
- > [stuff deleted]
-
- Got it to count chars in words but I doubt this is the best/most
- efficient way...
-
- suggestions greatly appreciated,
- Tamara
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- /* ---------------------- defines ------- */
- #define YES 1
- #define NO 0
- #define MAX 80
- /* ---------- function prototypes ------- */
- void Input();
- int NumWords();
- void OverUnder();
- /* --------------- global objects ------- */
- char test_string[MAX];
- int under, over, n;
- /* -------------------------main--------- */
- void main()
- {
- Input();
-
- printf("Number of words in the string: %d\n",
- NumWords());
- OverUnder();
- printf("Number of words with 7 or more char: %d\n"
- "Number of words with 6 or less char: %d\n",
- over, under);
- }
- /* ----get a character string input------ */
- void Input()
- {
- char buffer[300];
-
- printf("Enter test string: ");
- gets(buffer);
- strncpy(test_string, buffer, MAX);
- test_string[(MAX-1)]='\0';
- n = (strlen(test_string)+1); /* add 1 to get end null */
- }
- /* ----get number of words in the string-- */
- int NumWords()
- {
- int i, word_count, inword;
-
- inword=NO;
- word_count=0;
-
- for(i=0;i<n;i++) /* use of n shortens loop */
- {
- if(test_string[i]==' '||test_string[i]=='\n'
- ||test_string[i]=='\0'||test_string[i]=='\t')
- inword=NO;
- else if(inword==NO)
- {
- inword=YES;
- word_count++;
- }
- }
- return word_count;
- }
- /* count the nuber of char in each word and
- find qty less than or equal to 6
- and equal or greater than 7 */
- void OverUnder()
- {
- int i, char_count;
-
- char_count=0;
-
- for(i=0;i<n;i++)
- {
- char_count++;
- if(test_string[i]==' '||test_string[i]=='\n'
- ||test_string[i]=='\0'||test_string[i]=='\t')
- {
- if(((char_count-1)>0)&&((char_count-1)<7)) under++;
- if((char_count-1)>6) over++;
- char_count=0;
- }
- }
- }
-